package com.yc.chaoshi.cs_order.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yc.chaoshi.cs_order.mapper.OmsOrderProductMapper;
import com.yc.chaoshi.cs_order.mapper.PmsPlMapper;
import com.yc.chaoshi.entity.OmsOrderProduct;
import com.yc.chaoshi.entity.PmsPl;
import com.yc.common.api.CommonResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author XuShu
 * @since 2022-08-13
 */
@RestController
@RequestMapping("/chaoshi/pmsPl")
public class PmsPlController {
    @Resource
    PmsPlMapper pmsPlMapper;
    @Resource
    OmsOrderProductMapper orderProductMapper;
    @PostMapping("add")
    public CommonResult add(@RequestParam("uid") String uid,@RequestParam("gid") Integer gid,
                            @RequestParam("Evaluation") String Evaluation,@RequestParam("oid") String oid){
        PmsPl pmsPl=new PmsPl();
        pmsPl.setGid(gid);
        pmsPl.setPlword(Evaluation);
        pmsPl.setUid(Integer.valueOf(uid));
        pmsPl.setPltime(new Date());
        pmsPlMapper.insert(pmsPl);
        UpdateWrapper<OmsOrderProduct> updateWrapper=new UpdateWrapper<>();
        updateWrapper.lambda().eq(OmsOrderProduct::getProductId,gid)
                             .eq(OmsOrderProduct::getOrderId,oid)
                             .set(OmsOrderProduct::getCommentStatus,1);
        orderProductMapper.update(null,updateWrapper);
        return CommonResult.success(true);
    }
    // 查询当前商品评论
    @RequestMapping("findPl")
    public IPage<PmsPl> findPl(@RequestParam int id, @RequestParam int page){
        Page<PmsPl> ipage = new Page<>(page,3);
        ipage.setRecords(pmsPlMapper.findPl(id,(page-1)*3));
        final Map<String,Object> map = pmsPlMapper.countPl(id);
        long total = Long.parseLong(map.get("cnt") + "");
        ipage.setTotal(total);
        return ipage;
    }
}

